/**
 * Created by Administrator on 2017/10/17.
 */
//使用commonjs的规范引入mongoose模块，对mongodDB进行操作
var mongoose = require('mongoose');
//引入用户的数据
const  User=require('../models/user.model');
//用户的新增
exports.create=function (req,res,next) {
	const user=new User(req.body);
  user.save()
	  .then(data=>{
	  	res.json(data);
  })
}
//用户信息的获取
exports.get=function (req,res,next) {
	var id=req.params.id;
	User.findById(id,function (err,data) {
	res.json(data);
	})
}
//用户信息的更新
exports.updata=function (req,res,next) {
	const user=new  User(req.body);
	var id=req.params.id;
	User.findByIdAndUpdate(id,{$set:req.body},{new:false})
		.then(date=>{
			res.json(date);
		})
}
//用户信息的删除
exports.remove=function (req,res,next) {
	var id=req.params.id;
	User.findByIdAndRemove(id,function (err,data) {
		res.json(data);
	})
		.then(date=>{
			res.json(date);
		})
}
//用户信息的列表展示
exports.list=function (req,res,next) {
	var page = (req.body.page)?req.body.page:1;
	var rows =(req.body.limit)?req.body.limit:8;
	var queryCondition={};
	if (req.body.name&&req.body.name.trim().length>0){
		name=req.body.name;
		queryCondition={'name':new RegExp(name,'i')}
	}
	User.paginate(queryCondition,{sort:{_id:-1},page:parseInt(page),limit:parseInt(rows)},function (err,result) {
		result.rows=result.docs;
		delete result.docs;
		res.json(result);
	});
}
//用户信息的批量删除
exports.deletes=function (req,res,next) {
	var ids =req.body.ids;
	if (ids.length>0){
		console.log(req.body);
		User.remove({ _id:{$in:ids.split(',')}}).then(data=>{
			res.json({"msg":'删除成功！','status':200});
		})
	}
	else{
		res.json({"msg":'删除失败！','status':404})
	}
}
